<template>
  <el-dialog :visible.sync="visible" :title="$t('usercenter.encryptedTitle')" :close-on-click-modal="false"
    :close-on-press-escape="false" :append-to-body="true">
    <div class="step">
      <el-steps :active="active" finish-status="success" align-center>
        <el-step :title="$t('usercenter.stepTitle1')">
        </el-step>
        <el-step :title="$t('usercenter.stepTitle2')">
        </el-step>
      </el-steps>
    </div>
    <el-form v-show="active===0" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="next()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
      <el-form-item :label="$t('usercenter.question1')" prop="question1">
        <el-select v-model="dataForm.question1" :placeholder="$t('usercenter.question1')">
          <el-option v-for="question in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="question.val"
            :label="question.label" :value="question.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="answer1" :label="$t('usercenter.answer')">
        <el-input v-model="dataForm.answer1" type="answer1" :placeholder="$t('usercenter.answer')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('usercenter.question2')" prop="question2">
        <el-select v-model="dataForm.question2" :placeholder="$t('usercenter.question2')">
          <el-option v-for="question in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="question.val"
            :label="question.label" :value="question.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="answer2" :label="$t('usercenter.answer')">
        <el-input v-model="dataForm.answer2" type="answer2" :placeholder="$t('usercenter.answer')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('usercenter.question3')" prop="question3">
        <el-select v-model="dataForm.question3" :placeholder="$t('usercenter.question3')">
          <el-option v-for="question in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="question.val"
            :label="question.label" :value="question.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="answer3" :label="$t('usercenter.answer')">
        <el-input v-model="dataForm.answer3" type="answer3" :placeholder="$t('usercenter.answer')"></el-input>
      </el-form-item>
    </el-form>
    <!--确认信息-->
    <el-form v-show="active===1" :model="dataForm2" :rules="dataRule2" ref="dataForm2"
      @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <el-form-item :label="$t('usercenter.question1')" prop="question1">
        <el-select v-model="dataForm.question1" :placeholder="$t('usercenter.question1')" :disabled="true">
          <el-option v-for="question in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="question.val"
            :label="question.label" :value="question.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="answer1" :label="$t('usercenter.answer')">
        <el-input v-model="dataForm2.answer1" type="answer1" :placeholder="$t('usercenter.answer')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('usercenter.question2')" prop="question2">
        <el-select v-model="dataForm.question2" :placeholder="$t('usercenter.question2')" :disabled="true">
          <el-option v-for="question in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="question.val"
            :label="question.label" :value="question.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="answer2" :label="$t('usercenter.answer')">
        <el-input v-model="dataForm2.answer2" type="answer2" :placeholder="$t('usercenter.answer')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('usercenter.question3')" prop="question3">
        <el-select v-model="dataForm.question3" :placeholder="$t('usercenter.question3')" :disabled="true">
          <el-option v-for="question in this.$getDictList(this.TEMPLATE_TYPE, false)" :key="question.val"
            :label="question.label" :value="question.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="answer3" :label="$t('usercenter.answer')">
        <el-input v-model="dataForm2.answer3" type="answer3" :placeholder="$t('usercenter.answer')"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button v-if="active!==0" type="warning" @click="backt()">{{ $t('usercenter.backt') }}</el-button>
      <el-button v-if="active!==1" type="primary" @click="next()">{{ $t('usercenter.next') }}</el-button>
      <el-button v-if="active===1" type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<style scoped>
.step {
  margin-bottom: 50px;
}
</style>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      TEMPLATE_TYPE: 'ENCRYPTED_QUESTION',
      active: 0,
      visible: false,
      dataForm: {
        question1: '',
        answer1: '',
        question2: '',
        answer2: '',
        question3: '',
        answer3: ''
      },
      dataForm2: {
        answer1: '',
        answer2: '',
        answer3: ''
      }
    }
  },
  computed: {
    dataRule () {
      var validateQuestion1 = (rule, value, callback) => {
        if (this.dataForm.question2 === value || this.dataForm.question3 === value) {
          return callback(new Error(this.$t('usercenter.err1')))
        }
        callback()
      }
      var validateQuestion2 = (rule, value, callback) => {
        if (this.dataForm.question1 === value || this.dataForm.question3 === value) {
          return callback(new Error(this.$t('usercenter.err1')))
        }
        callback()
      }
      var validateQuestion3 = (rule, value, callback) => {
        if (this.dataForm.question1 === value || this.dataForm.question2 === value) {
          return callback(new Error(this.$t('usercenter.err1')))
        }
        callback()
      }
      return {
        answer1: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        answer2: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        answer3: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        question1: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateQuestion1, trigger: 'blur' }
        ],
        question2: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateQuestion2, trigger: 'blur' }
        ],
        question3: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateQuestion3, trigger: 'blur' }
        ]
      }
    },
    dataRule2 () {
      var validateAnswer1 = (rule, value, callback) => {
        if (this.dataForm.answer1 !== value) {
          return callback(new Error(this.$t('usercenter.err2')))
        }
        callback()
      }
      var validateAnswer2 = (rule, value, callback) => {
        if (this.dataForm.answer2 !== value) {
          return callback(new Error(this.$t('usercenter.err2')))
        }
        callback()
      }
      var validateAnswer3 = (rule, value, callback) => {
        if (this.dataForm.answer3 !== value) {
          return callback(new Error(this.$t('usercenter.err3')))
        }
        callback()
      }
      return {
        answer1: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateAnswer1, trigger: 'blur' }

        ],
        answer2: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateAnswer2, trigger: 'blur' }
        ],
        answer3: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { validator: validateAnswer3, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.active = 0
        this.$refs['dataForm'].resetFields()
        this.$refs['dataForm2'].resetFields()
      })
    },
    next () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.active++
      })
    },
    backt () {
      this.active--
      this.$refs['dataForm2'].resetFields()
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm2'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.put('/user/center/question', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshData')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
